<template>
  <div class="ma-content-block lg:flex justify-between">
    <!-- CRUD 组件 -->
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item field="name" label="任务名称">
            <a-input v-model="searchForm.name" placeholder="请输入任务名称" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="type" label="任务类型">
            <a-select v-model="searchForm.type" :options="types" allow-clear placeholder="请选择任务类型" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="status" label="状态">
            <sa-select v-model="searchForm.status" dict="data_status" allow-clear placeholder="请选择状态" />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
      <!-- 状态列 -->
      <template #status="{ record }">
        <sa-switch v-model="record.status" @change="changeStatus($event, record.id)"></sa-switch>
      </template>

      <!-- 操作前置扩展 -->
      <template #operationBeforeExtend="{ record }">
        <a-popconfirm content="确定立刻执行一次?" position="bottom" @ok="run(record)">
          <a-link v-auth="['/core/crontab/run']"><icon-caret-right /> 执行一次</a-link>
        </a-popconfirm>
        <a-link @click="openLogModal(record)"><icon-history /> 日志 </a-link>
      </template>
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />

    <!-- 日志记录 -->
    <log-list ref="logsRef" />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { Message } from '@arco-design/web-vue'

import api from '@/api/setting/crontab'
import LogList from './logList.vue'
import EditForm from './edit.vue'

const crudRef = ref()
const editRef = ref()
const logsRef = ref()

const searchForm = ref({
  name: '',
  type: '',
  status: '',
})

const types = [
  { label: 'URL任务GET', value: 1 },
  { label: 'URL任务POST', value: 2 },
  { label: '类任务', value: 3 },
]

const changeStatus = async (status, id) => {
  const response = await api.changeStatus({ id, status })
  if (response.code === 200) {
    Message.success(response.message)
    crudRef.value.refresh()
  }
}

const run = async (row) => {
  const response = await api.run({ id: row.id })
  if (response.code === 200) {
    Message.success(response.message)
    crudRef.value.refresh()
  }
}

const openLogModal = (row) => {
  logsRef.value.open(row.id)
}

const options = reactive({
  api: api.getPageList,
  rowSelection: { showCheckedAll: true },
  operationColumnWidth: 280,
  add: {
    show: true,
    auth: ['/core/crontab/save'],
    func: async () => {
      editRef.value?.open()
    },
  },
  edit: {
    show: true,
    auth: ['/core/crontab/update'],
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: true,
    auth: ['/core/crontab/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
  },
})

const columns = reactive([
  { title: '任务名称', dataIndex: 'name', width: 200 },
  { title: '任务类型', dataIndex: 'type', options: types, width: 150 },
  { title: '定时规则', dataIndex: 'rule', width: 150 },
  { title: '调用目标', dataIndex: 'target', width: 260 },
  { title: '状态', dataIndex: 'status', dict: 'data_status', width: 120 },
  { title: '创建时间', dataIndex: 'create_time', width: 180 },
])

const initPage = async () => {}

const refresh = async () => {
  crudRef.value?.refresh()
}

onMounted(async () => {
  initPage()
  refresh()
})
</script>
